extends Node2D
class_name StateMachine


var currentStateDict:Dictionary = {}
var stateDict:Dictionary = {}
var stateArr:Array = []


func _ready():
	currentStateDict = {}
	stateDict = {}
	stateArr = []
	var childCount = get_child_count( )
	for index in range(childCount):
		var childStateNode = get_child(index)
		stateDict[childStateNode.name] = childStateNode
		stateArr.append(childStateNode)
	

func run(delta, ownerNode):
	for key in currentStateDict.keys():
		var targetState = currentStateDict[key]
		targetState.run(delta, ownerNode)


func addState(stateName, ownerNode):
	var targetState:StateBase = stateDict[stateName]
	if not currentStateDict.has(stateName):
		currentStateDict[stateName] = targetState
		targetState.enter(ownerNode)
	

func removeState(stateName, ownerNode):
	var targetState:StateBase = currentStateDict[stateName]
	if targetState != null:
		targetState.exit(ownerNode)
		currentStateDict.erase(stateName)


